//
//  historyTool.m
//  欢乐美食
//
//  Created by 遇水 on 15/6/27.
//  Copyright (c) 2015年 遇水. All rights reserved.
//

#import "historyTool.h"

#define HistoryFile  [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"historyFile.data"]

#define  colelctesFile [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"collectesFile.data"]

@interface historyTool ()

@property (nonatomic, strong) NSMutableArray    *history;
@property (nonatomic, strong) NSMutableArray    *collectes;

@end

@implementation historyTool
HMSingletonM(historyTool)

-(NSMutableArray *)history
{
    if (!_history) {
        _history = [NSKeyedUnarchiver unarchiveObjectWithFile:HistoryFile];
        if (!_history) {
            _history = [NSMutableArray array];
        }
    }
    return _history;
}


-(NSMutableArray *)collectes{
    if (_collectes==nil) {
        _collectes = [NSKeyedUnarchiver unarchiveObjectWithFile:colelctesFile];
        if (!_collectes) {
            _collectes = [NSMutableArray array];
        }
    }
    
    return _collectes;
}




-(void)saveHistoryDeal:(MSDeals *)deal
{

    [self.history removeObject:deal];   //首先删除已有的
//    [self.history addObject:deal];
    //此处应该是插入
    [self.history insertObject:deal atIndex:0]; //将现在传入的插入到第一个
    
   [NSKeyedArchiver archiveRootObject:self.history toFile:HistoryFile];
}
//删除历史记录
-(void)unsaveHistoryDeal:(MSDeals *)deal
{
    [self.history removeObject:deal];
    
    [NSKeyedArchiver archiveRootObject:self.history toFile:HistoryFile];
}

//保存收藏的
-(void)saveCollectes:(MSDeals *)deal
{
    [self.collectes removeObject:deal];
    [self.collectes insertObject:deal atIndex:0];
    
    [NSKeyedArchiver archiveRootObject:self.collectes toFile:colelctesFile];
}


-(void)unsaveCollectes:(MSDeals *)deal
{
    [self.collectes removeObject:deal];
    
    [NSKeyedArchiver archiveRootObject:self.collectes toFile:colelctesFile];
}







@end
